import { ShaohuoPersonService } from 'services/Api'
import { DateFormat } from 'services/Util'

/* 搜索相关
-------------------------- */
export const listSearchMixin = {
  data () {
    return {
      search: '', // 搜索内容
      ustId: '',
      applyStatus: '',
      examStatus: '',
      shaohuoPersonStatus: '',
      startTime: '',
      endTime: ''
    }
  },
  methods: {
    // 搜索条件改变
    searchCondChange (list) {
      this.search = ''
      this.ustId = ''
      this.applyStatus = ''
      this.examStatus = ''
      this.shaohuoPersonStatus = ''
      this.startTime = ''
      this.endTime = ''
      list.forEach(i => {
        switch (i.name) {
          case '模糊查询':
            this.search = i.value
            break
          case '用户类型':
            this.ustId = i.value
            break
          case '审核状态':
            this.applyStatus = i.value
            break
          case '考试状态':
            this.examStatus = i.value
            break
          case '捎货人状态':
            this.shaohuoPersonStatus = i.value
            break
          case '注册时间':
            this.startTime = DateFormat(i.value[0], 'yyyy-MM-dd hh:mm:ss') || ''
            this.endTime = DateFormat(i.value[1], 'yyyy-MM-dd hh:mm:ss') || ''
        }
      })
      this.getTableData()
    }
  }
}

/* 状态相关（恢复禁用按钮）
-------------------------- */
export const listStatusMixin = {
  data () {
    return {
      status: '' // 状态:1为有效,0为禁用
    }
  },
  computed: {
    // 修改状态参数
    statusParam () {
      return {
        acc_id: this.acc_id,
        ids: this.selectIds,
        status: this.status
      }
    }
  },
  methods: {
    // 改变状态接口
    currentStatusApi () {
      return ShaohuoPersonService.alterShaohuoUserStatus(this.statusParam)
    }
  }
}

/* 审核相关
-------------------------- */
export const listApplyMixin = {
  data () {
    return {
      apply: '', // 能否提现:0为允许,1为不允许,''不改变
      remark: '',
      applyIndex: '',
      currentApply: '', // 当前提现操作
      applyPopover: false,
      applyList: [
        { name: '通过', value: '2' },
        { name: '拒绝', value: '3' }
      ]
    }
  },
  computed: {
    // 修改审核参数
    applyParam () {
      return {
        acc_id: this.acc_id,
        ids: this.selectIds,
        status: this.apply,
        remark: this.remark
      }
    }
  },
  methods: {
    // 用户选择完毕
    async applyChange () {
      this.applyPopover = true
      // 如果选择拒绝，还需要填写拒绝原因
      if (this.apply === '3') {
        const validator = (v) => {
          return !!v
        }
        try {
          const res = await this.PromptTemp('拒绝时需要提交审核备注', validator, '请输入审核备注')
          this.confirmApply(res.value)
        } catch (e) {
          // 用户关闭了输入框, 重置
          this.apply = ''
          this.remark = ''
        }
      } else {
        this.confirmApply()
      }
    },
    // 确认提交
    confirmApply (v) {
      if (v) this.remark = v
      typeof this.applyIndex === 'number'
        ? this.AsyncLoading('审核', this.applyIndex, this.applyApi)
        : this.AsyncLoading('审核', this.applyApi)
    },
    // 审核单个
    applyOne (id, index) {
      this.selectIds = id
      this.applyIndex = index
    },
    // 批量审核已选的数据(idKey: 行数据中id对应的字段名)
    applySelected (idKey) {
      if (!this.selectList.length) return this.$message.warning('选择数据后才能操作')
      this.selectIds = this.selectList.map(item => item[idKey]).join(',')
      this.applyIndex = ''
    },
    // 改变审核接口
    currentApplyApi () {
      return ShaohuoPersonService.applyShaohuoUser(this.applyParam)
    },
    // 接口
    async applyApi () {
      const [ err ] = await this.currentApplyApi()
      this.apply = ''
      this.remark = ''
      if (err) return this.$message.error('操作失败')
      this.$message.success(`操作成功`)
      this.getTableData()
    }
  }
}

export const listExportMixin = {
  computed: {
    // 导出参数
    exportShaohuoUserParam () {
      return {
        acc_id: this.acc_id,
        sort_field: this.sort_field,
        sort_type: this.sort_type,
        search: this.search,
        ust_id: this.ustId,
        apply_status: this.applyStatus,
        exam_status: this.examStatus,
        status: this.shaohuoPersonStatus,
        start_time: this.startTime,
        end_time: this.endTime
      }
    }
  },
  methods: {
    // 导出接口
    currentExportApi () {
      return ShaohuoPersonService.exportShaohuoUser(this.exportShaohuoUserParam)
    }
  }
}
